package com.bc.mcode.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor  {
	private final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);
	public static final String LAST_PAGE = "com.alibaba.lastPage";
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
	    String requestUri = request.getRequestURI();
	    String contextPath = request.getContextPath();
	    String url = requestUri.substring(contextPath.length());
		//SysUserVO user =  (SysUserVO) request.getSession().getAttribute(Constants.USERVO_SESSION_KEY);
		Object user = new Object();
		if(user == null){
			//如果是登录页面 则不拦截
			if(url.indexOf("login") > 0){
				return true;
			}
			request.getRequestDispatcher("/system/login").forward(request, response);
			return false;
		}else
			return true;
	}

}
